<?php
include("../contents.php");
include("../security.php");
include("../settings.php");
include("../database.php");
include("../statistics.php");
include("../xml.php");
$GLOBALS["add_xml"]=array();
$contents=new Contents(1);
if ($contents->checkedin==1) {
	$doc=new XML(0);
} else {
	$doc=new XML(1);
}
if (!empty($_GET["delrepetition"])) {
	mysql_query("UPDATE ". $GLOBALS["db_pref"] ."content_repetitions SET deleted='1', delete_by='". $_SESSION["userid"] ."', delete_date='". idate("U") ."' WHERE id='". $_GET["delrepetition"] ."'");
	forward_self();
}

if (!empty($_GET["addrepetition"])) {
	$maxsort=mysql_query("SELECT max(`sort`) as maxsort FROM ". $GLOBALS["db_pref"] ."content_repetitions WHERE docuid='". $contents->docuid2 ."' and name='". $_GET["addrepetition"] ."' and deleted='0'");
	if (mysql_num_rows($maxsort)>0) {
		$rowmaxsort=mysql_fetch_object($maxsort);
		$maxsort=$rowmaxsort->maxsort;
	} else {
		$maxsort=0;
	}
	$newid=create_uniqid($GLOBALS["db_pref"] ."content_repetitions");
	$doclang=mysql_query("SELECT editlang FROM ". $GLOBALS["db_pref"] ."content_documents WHERE id2='". $contents->docuid2 ."'");
	if (mysql_num_rows($doclang)==1) {
		$row_lang=mysql_fetch_object($doclang);
		$editlang=$row_lang->editlang;
	} else {
		$editlang=getsetting("standard_language");
	}
	mysql_query("INSERT INTO ". $GLOBALS["db_pref"] ."content_repetitions SET id2='". $newid ."', name='". $_GET["addrepetition"] ."', docuid='". $contents->docuid2 ."', language='". $editlang ."', `sort`='". ($maxsort + 1) ."', created_by='". $_SESSION["userid"] ."', created_date='". idate("U") ."'");
	// forward_self();
}
if(!empty($_GET["move_rep_up"])) {
	$act_sorter=mysql_query("SELECT `id`, `sort`, `docuid` FROM ". $GLOBALS["db_pref"] ."content_repetitions WHERE id='". $_GET["move_rep_up"] ."'");
	$row_act_sort_rep=mysql_fetch_object($act_sorter);
	$next_sorter=mysql_query("SELECT `id`, `sort` FROM ". $GLOBALS["db_pref"] ."content_repetitions WHERE `docuid`='". $row_act_sort_rep->docuid ."' and `sort`<'". $row_act_sort_rep->sort ."' and deleted='0' ORDER BY `sort` DESC LIMIT 1");
	$row_next_sort_rep=mysql_fetch_object($next_sorter);
	mysql_query("UPDATE ". $GLOBALS["db_pref"] ."content_repetitions SET `sort`='". $row_next_sort_rep->sort ."' WHERE id='". $row_act_sort_rep->id ."'");
	mysql_query("UPDATE ". $GLOBALS["db_pref"] ."content_repetitions SET `sort`='". $row_act_sort_rep->sort ."' WHERE id='". $row_next_sort_rep->id ."'");
	forward_self();
}

if(!empty($_GET["move_rep_down"])) {
	$act_sorter=mysql_query("SELECT `id`, `sort`, `docuid` FROM ". $GLOBALS["db_pref"] ."content_repetitions WHERE id='". $_GET["move_rep_down"] ."'");
	$row_act_sort_rep=mysql_fetch_object($act_sorter);
	$next_sorter=mysql_query("SELECT `id`, `sort` FROM ". $GLOBALS["db_pref"] ."content_repetitions WHERE `docuid`='". $row_act_sort_rep->docuid ."' and `sort`>'". $row_act_sort_rep->sort ."' and deleted='0' ORDER BY `sort` LIMIT 1");
	$row_next_sort_rep=mysql_fetch_object($next_sorter);
	mysql_query("UPDATE ". $GLOBALS["db_pref"] ."content_repetitions SET `sort`='". $row_next_sort_rep->sort ."' WHERE id='". $row_act_sort_rep->id ."'");
	mysql_query("UPDATE ". $GLOBALS["db_pref"] ."content_repetitions SET `sort`='". $row_act_sort_rep->sort ."' WHERE id='". $row_next_sort_rep->id ."'");
	forward_self();
}

/*
if (!empty($_GET["editrepetition"]) && !empty($_GET["editblock"])) {
	$queryfield=mysql_query("SELECT * FROM ". $GLOBALS["db_pref"] ."content_blocks WHERE repetition='". $_GET["editrepetition"] ."' and name='". $_GET["editblock"] ."' and deleted='0'");
	if (mysql_num_rows($queryfield)<1) {
		mysql_query("INSERT INTO ". $GLOBALS["db_pref"] ."content_blocks SET repetition='". $_GET["editrepetition"] ."', name='". $_GET["editblock"] ."', content='', created_by='". $_SESSION["userid"] ."', created_date='". idate("U") ."'");
	}
}
*/
if (!empty($_GET["select_block_template"]) && !empty($_GET["repetitionid"])) {
	$field="";
	if (substr($_GET["select_block_template"],0,4)=="mod_") {
		$field="template='', module='". substr($_GET["select_block_template"],4,(strlen($_GET["select_block_template"]) - 4)) ."'";
	} else {
		$field="template='". $_GET["select_block_template"] ."', module=''";
	}
	$queryfield=mysql_query("UPDATE ". $GLOBALS["db_pref"] ."content_repetitions SET ". $field ." WHERE id='". $_GET["repetitionid"] ."' and deleted='0'");
	forward_self();
}

/*
if (!empty($_POST["rte1"])) {
	mysql_query("UPDATE ". $GLOBALS["db_pref"] ."content_blocks SET deleted='1', delete_by='". $_SESSION["userid"] ."', delete_date='". idate("U") ."' WHERE repetition='". $_POST["edittextblockrepetition"] ."' and name='". $_POST["edittextblock"] ."'");
	mysql_query("INSERT INTO ". $GLOBALS["db_pref"] ."content_blocks SET repetition='". $_POST["edittextblockrepetition"] ."', name='". $_POST["edittextblock"] ."', content='". rteSafe($_POST["rte1"]) ."', created_by='". $_SESSION["userid"] ."', created_date='". idate("U") ."'");
	forward_self();
}
*/
$doc->add_node("presence", $contents->actpresence);
$doc->add_document_infos($contents);
$doc->array2xml($contents->get_documents());
$doc->navarray2xml($contents->get_navigation());
$doc->getblocktemplates();
$doc->getmodules($contents->actpresence);
$doc->output($contents->template, 0, null, 0, $contents->pagetitle);

function forward_self() {
	$zusatz="";
	if (!empty($_GET["name"])) {
		$zusatz.="&name=". $_GET["name"];
	}
	if (!empty($_GET["dirname"])) {
		$zusatz.="&dirname=". $_GET["dirname"];
	}
	// echo "Location:". $_SERVER["PHP_SELF"] ."?". $zusatz;
	header("Location:". $_SERVER["PHP_SELF"] ."?". $zusatz);
	exit();
}
/*
function rteSafe($strText) {
	//returns safe code for preloading in the RTE
	$tmpString = $strText;
	
	//convert all types of single quotes
	$tmpString = str_replace(chr(145), chr(39), $tmpString);
	$tmpString = str_replace(chr(146), chr(39), $tmpString);
	$tmpString = str_replace("'", "&#39;", $tmpString);
	
	//convert all types of double quotes
	$tmpString = str_replace(chr(147), chr(34), $tmpString);
	$tmpString = str_replace(chr(148), chr(34), $tmpString);
//	$tmpString = str_replace("\"", "\"", $tmpString);
	
	//replace carriage returns & line feeds
	$tmpString = str_replace(chr(10), " ", $tmpString);
	$tmpString = str_replace(chr(13), " ", $tmpString);
	
	return $tmpString;
} */
?>
<?php
function create_uniqid($table) {
	$newid=md5(uniqid(rand()));
	if (mysql_num_rows(mysql_query("SELECT id2 FROM ". $table ." WHERE id2='". $newid ."'"))>0) {
		return create_uniqid($table);
	} else {
		return $newid;
	}
}
?>